﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public class GestionZone
    {
        public void Ajouter(Zone zone)
        {
            try
            {
                using (Context db = new Context())
                {
                    Restaurant resto = db.Restaurants.Where(r => r.Id == zone.RestaurantId).FirstOrDefault();

                    zone.Restaurant = resto;

                    db.Zones.AddObject(zone);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public Zone Chercher(int zoneId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Zones.Include("Restaurant").Include("Serveur").Include("Table").
                        Where(z => z.Id == zoneId).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Zone Chercher(Serveur serveur)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from z in db.Zones
                            where z.Serveur.Id == serveur.Id
                            select z).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Zone Chercher(string email)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from z in db.Zones
                            where z.Serveur.Email == email
                            select z).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Zone> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Zones.ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Zone> Lister(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from z in db.Zones.
                                Include("Table").
                                Include("Serveur")
                            where z.Restaurant.Id == restaurant.Id
                            select z).ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MettreAJour(Zone zone)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Zones.Attach(zone);
                    db.ObjectStateManager.ChangeObjectState(zone, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(int id)
        {
            try
            {
                using (Context db = new Context())
                {
                    Zone zone = db.Zones.Where(z => z.Id == id).FirstOrDefault();

                    db.ObjectStateManager.ChangeObjectState(zone, System.Data.EntityState.Deleted);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
